﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Core
{
    public class ValidationErrorCollection : IValidationErrorCollection
    {
        private ICollection<IValidationError> _errors;

        public ValidationErrorCollection()
        {
            this._errors = new HashSet<IValidationError>();
        }

        public void Add(IValidationError error)
        {
            error.ThrowIfNull("error");
            this._errors.Add(error);
        }

        public void Add(string message, object target, string propertyName)
        {
            IValidationError error = new ValidationError(message, target, propertyName);
            this.Add(error);
        }

        public void AddAllFrom(IValidationErrorCollection errorCollection)
        {
            if (errorCollection != null)
            {
                foreach (IValidationError error in errorCollection)
                {
                    this._errors.Add(error);
                }
            }
        }

        public bool Remove(object target, string propertyName)
        {
            IValidationError error = this._errors.Where(e => Object.Equals(target, e.Target) && String.Equals(propertyName, e.PropertyName)).SingleOrDefault();
            return error != null && this._errors.Remove(error);
        }

        public int Count
        {
            get { return this._errors.Count; }
        }

        public bool IsValid
        {
            get { return this.Count == 0; }
        }

        #region IEnumerable<IErrorNotification> Members

        public IEnumerator<IValidationError> GetEnumerator()
        {
            return this._errors.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}
